package com.modular.common;

import org.springframework.security.core.GrantedAuthority;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserGrantedAuthority implements GrantedAuthority {
    private  String authority;
    private static final Map<String, UserGrantedAuthority> USER_GRANTED_AUTHORITY_CONCURRENT = new ConcurrentHashMap();

    public UserGrantedAuthority(String authority){
        this.authority = authority;
    }

    public static GrantedAuthority getInstance(String authority) {
        UserGrantedAuthority userGrantedAuthority = USER_GRANTED_AUTHORITY_CONCURRENT.get(authority);
        if (userGrantedAuthority == null) {
            userGrantedAuthority = new UserGrantedAuthority(authority);
            USER_GRANTED_AUTHORITY_CONCURRENT.put(authority, userGrantedAuthority);
        }
        return userGrantedAuthority;
    }


    @Override
    public String getAuthority() {
        return this.authority;
    }
}
